package com.blb.controller;

import com.blb.entity.Comtype;
import com.blb.entity.Menu;
import com.blb.service.IProductTypeService;
import com.blb.utils.IconfontUtils;
import com.blb.utils.LayuiPage;
import com.blb.utils.UUIDUtils;
import org.apache.commons.io.FileUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("product")
public class ProductTypeController {

    @Autowired
    private IProductTypeService productTypeService;

    private static String filePath = "D:/file_upload/";


    /**
     * 跳转商品分类
     *
     * @return
     */
    @RequestMapping("index")
    public String index(Model model, @Param("menuid") String menuid) {
        List<Menu> list = productTypeService.bts(menuid);
        model.addAttribute("bts", list);
        return "admin/productType/product_type";
    }


    /**
     * 商品分类分页
     *
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping("page")
    public LayuiPage page(int page, int limit, String id, String name) {
        LayuiPage pagelist = null;
        try {
            pagelist = productTypeService.page((page - 1) * limit, limit, id, name);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return pagelist;
    }

    /**
     * 显示图片
     *
     * @param name
     * @return
     * @throws IOException
     */
    @RequestMapping("getImage")
    public ResponseEntity<byte[]> getImage(String name) throws IOException {
        File file = new File(filePath + name);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.IMAGE_JPEG);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), httpHeaders, HttpStatus.OK);
    }

    /**
     * 跳转添加
     *
     * @return
     * @throws IOException
     */
    @RequestMapping("showAdd")
    public String roleList(Model model, HttpServletRequest request) throws IOException {
        List<String> iconfonts = IconfontUtils.getIconfonts(request);
        model.addAttribute("iconfonts", iconfonts);
        return "admin/productType/productType_add";
    }

    /**
     * 添加商品分类
     *
     * @param comtype
     * @return
     */
    @ResponseBody
    @RequestMapping("add")
    public String add(Comtype comtype) {
        comtype.setId(UUIDUtils.getID());
        try {
            productTypeService.add(comtype);
        } catch (Exception e) {
            e.printStackTrace();
            return "添加失败" + e.getMessage();
        }
        return "";
    }

    /**
     * 判断商品类别或序号是否存在
     *
     * @param
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("nameisExist")
    public String nameisExist(@Param("name") String name) throws IOException {

        if (name != null) {
            List<Comtype> list = productTypeService.nameisExist(name);
            if (!list.isEmpty()) {
                return "商品类别已存在";
            }
        }
        return "";
    }

    /**
     * 判断商品类别或序号是否存在
     *
     * @param
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("seqisExist")
    public String seqisExist(int seq) throws IOException {

        if (seq + "" != null) {
            List<Comtype> list = productTypeService.seqisExist(seq);
            if (!list.isEmpty()) {
                return "序号已存在";
            }
        }
        return "";
    }

    /**
     * 跳转修改
     *
     * @param model
     * @param
     * @return
     * @throws IOException
     */
    @RequestMapping("showUpdate")
    public String updateRole(Model model, Comtype comtype, HttpServletRequest request) throws IOException {
        model.addAttribute("comtype", comtype);
        List<String> iconfonts = IconfontUtils.getIconfonts(request);
        model.addAttribute("iconfonts", iconfonts);
        return "admin/productType/productType_update";
    }

    /**
     * 修改
     *
     * @param comtype
     * @return
     */
    @ResponseBody
    @RequestMapping("update")
    public String update(Comtype comtype) {
        try {
            productTypeService.update(comtype);
        } catch (Exception e) {
            e.printStackTrace();
            return "修改失败" + e.getMessage();
        }
        return "";
    }


    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("delete")
    public String delete(@RequestBody List<String> ids) {
        String msg = "";
        try {
            productTypeService.delete(ids);
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return msg;
    }
}
